<?php
session_start();
// SESSION INFO//////////////////////////////////////////////////////////////////////////////////

// USE THE CURRENT SESSION TO DETERMINE QUESTION SHOWN
$currentQuestion = isset($_SESSION['currentQuestion']) ? $_SESSION['currentQuestion'] : 1;

	
			?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Multiple Choice Assessment</title>
<link href="styles/styles.css" rel="stylesheet" type="text/css">
<script src="scripts/scripts.js" type="text/javascript"></script>
</head>
<body>
<form name="questions" action="test2.php" method="post">
<?php
/////////////////////////////////////////////////////////////////////////////////////////
//
// GET QUESTION FROM DB                                                           
//
/////////////////////////////////////////////////////////////////////////////////////////

if (!isset($_POST['checkAnswer']) && !isset($_POST['nextQuestion'])) {
$server = mysql_connect ('localhost', 'lr12aas', 'u3CJFB4r');
mysql_select_db("dblr12aas", $server);
	if (mysqli_connect_errno()) {
			printf ("Connect failed: %s <br />",
			mysqli_connect_error());
			echo "<p class='crying'>Connection problem</p>";
			exit ();
			}
				else {
			echo "<p class='smiling'>Connected</p>";			
		}
$question = mysql_query("SELECT * "
. "FROM questions WHERE id ="
. $_SESSION['currentQuestion']."");
$x = 0;
while ($row = mysql_fetch_assoc($question))
{
   echo $row['question'] . '<br />';
   echo '<input type="radio" name="a'.$x.'" value=1 />' .$row['answer1'] . '<br />';
   echo '<input type="radio" name="a'.$x.'" value=2 />' .$row['answer2'] . '<br />';
   echo '<input type="radio" name="a'.$x.'" value=3 />' .$row['answer3'] . '<br />';
   echo '<input type="radio" name="a'.$x.'" value=4 />' .$row['answer4'] . '<br />';
   echo '<input type="submit" name="checkAnswer" value="Check Answer" />';
   $x = $x + 1;
}
}
else
{
	echo '<br/>';
	}
?>
</form>


<form name="feedback<?php echo $currentQuestion ?>" action="test2.php" method="post">
<?php 
/////////////////////////////////////////////////////////////////////////////////////////
//
// handle answer                                                         
//
/////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['checkAnswer'])) {

////WRITE THE SESSION NO TO THE PAGE TO VIEW IT   
		echo "<p class='smiling'>Session: " . $currentQuestion . "</p>";	

$server = mysql_connect ('localhost', 'lr12aas', 'u3CJFB4r');
mysql_select_db("dblr12aas", $server);
	if (mysqli_connect_errno()) {
			printf ("Connect failed: %s <br />",
			mysqli_connect_error());
			echo "<p class='crying'>Connection problem</p>";
			exit ();
			}
				else {
			echo "<p class='smiling'>Connected</p>";			
		}
$question = mysql_query("SELECT * "
. "FROM questions WHERE id ="
. $_SESSION['currentQuestion']."");
$x = 0;
$score = 0;
while ($row = mysql_fetch_assoc($question))
{
    echo $row['question'] . '?<br />';

    $answered = $row['answer'.$_POST['a'.$x]] ;
    $correct = $row['correct'] ;

    if ($answered == $correct ) {
        $score++;
        $acolor = 'green' ;
    }
    else {
        $acolor = 'red' ;
    }

    echo 'you answered <font color=' . $acolor . '>' . $answered . '<font color=black> <br />';


    echo 'the correct answer was ' . $correct . '<br />' ;
echo '<br /><br />' ;

    $x = $x + 1;
}

echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';


}
else
{
	echo '<br/>';
	}

?>
</form>
</body>
</html>

